//
//  NLSubTableViewController.m
//  NLScrollPagination
//
//  Created by noahlu on 14-8-1.
//  Copyright (c) 2014年 noahlu<codedancerhua@gmail.com>. All rights reserved.
//

#import "NLSubTableViewController.h"
#import "GroupTopTabView.h"
#import "NLMainTableViewController.h"
#import "UIColor+Additions.h"

#define NAV_BAR_HEIGHT 44



@interface NLSubTableViewController () <GroupTopTabViewDelegate>

- (void)initComponents;

@end

@implementation NLSubTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setEdgesForExtendedLayout:UIRectEdgeNone];
    [self initComponents];
}

- (void)addRefreshView:(UITableView *)tableView{
    if (self.pullFreshView == nil) {
        self.pullFreshView = [[NLPullDownRefreshView alloc]initWithFrame:CGRectMake(0, -38.f, self.view.frame.size.width, 38.f)];
        self.pullFreshView.backgroundColor = [UIColor whiteColor];
    }
    
    if (!self.pullFreshView.superview) {
        [self.pullFreshView setupWithOwner:tableView delegate:(id<NLPullDownRefreshViewDelegate>)self.mainTableViewController];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.pullFreshView scrollViewWillBeginDragging:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.pullFreshView scrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.pullFreshView scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}

#pragma mark 
- (void)initComponents
{
    self.view.backgroundColor = [UIColor colorWithHex:0xF1F1F1 alpha:1.0];
    
    CGRect rectContent = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-NAV_BAR_HEIGHT-64);
    
    self.tableView01 = [[UITableView alloc] initWithFrame:rectContent];
//    self.tableView02 = [[UITableView alloc] initWithFrame:rectContent];
//    self.tableView03 = [[UITableView alloc] initWithFrame:rectContent];

    [self.tableView01 setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView01 setBackgroundColor:[UIColor clearColor]];
//    [self.tableView02 setSeparatorStyle:UITableViewCellSeparatorStyleNone];
//    [self.tableView02 setBackgroundColor:[UIColor clearColor]];
//    [self.tableView03 setSeparatorStyle:UITableViewCellSeparatorStyleNone];
//    [self.tableView03 setBackgroundColor:[UIColor clearColor]];
    
    [self.view addSubview:self.tableView01];
//    [self.view addSubview:self.tableView02];
//    [self.view addSubview:self.tableView03];
    
    [self addRefreshView:self.tableView01];
    
//    GroupTopTabView *topTabView = [[GroupTopTabView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)
//                                                          withTitleArray:nil];
    
//    topTabView.delegate = self;
//    [topTabView setSelectedIndex:GroupTypePast];
//    [self cellDidSelected:GroupTypePast];
//    
//    [self.view addSubview:topTabView];
}

- (void)cellDidSelected:(GroupType)type
{
    [self.pullFreshView removeFromSuperview];
    if (type==GroupTypePast) {
        [self.tableView01 setHidden:NO];
        [self.tableView02 setHidden:YES];
        [self.tableView03 setHidden:YES];
        
        [self addRefreshView:self.tableView01];
    }
    else if (type == GroupTypeCurrent)
    {
        [self.tableView01 setHidden:YES];
        [self.tableView02 setHidden:NO];
        [self.tableView03 setHidden:YES];
        
        if ([self.mainTableViewController isKindOfClass:[NLMainTableViewController class]]) {

        }
        
        [self addRefreshView:self.tableView02];
    }
    else{
        [self.tableView01 setHidden:YES];
        [self.tableView02 setHidden:YES];
        [self.tableView03 setHidden:NO];
        
        [self addRefreshView:self.tableView03];
    }
    
    [self.tableView01 reloadData];
    [self.tableView02 reloadData];
    [self.tableView03 reloadData];
}

@end
